
                                   #���������滪�᰸���ݷ���

library(ggplot2)
library(reshape)
library(plm)
library(pglm)



#��������
#ԭʼ����
load("US20200610.RData")
#��������������������
load("state.RData")



    ##���ĵ�2��3�ڵļ��㡢ͼ������ 

##�᰸����
#����
attach(US20200610)
prop.table(table(�᰸����))
table(�᰸����)
#����ͼ1 ��ͬ�����᰸�ķֲ�ͼ
ggplot(US20200610, aes(x=�᰸����)) + geom_bar()+ylab("����")+
  scale_x_discrete(limits=c("�鰸","���Ͼ��鰸","������","�򵥾��鰸","��ͬ���鰸"))+
  annotate("text", x="�鰸", y=385, label="51.3%", alpha=1.5,size = 4)+
  annotate("text", x="���Ͼ��鰸", y=20, label="0.7%", alpha=1.5,size = 4)+
  annotate("text", x="������", y=50, label="4.5%", alpha=1.5,size = 4)+
  annotate("text", x="�򵥾��鰸", y=240, label="31.1%", alpha=1.5,size = 4)+
  annotate("text", x="��ͬ���鰸", y=103, label="12.2%", alpha=1.5,size = 4)
ggsave("�᰸������״ͼ��.png", width=12, height=8, unit="cm", dpi=300)



##�᰸�漰����
#1. ����
prop.table(table(�漰����))

#2. ����ͼ3���᰸�漰����ķֲ�ͼ
ggplot(US20200610, aes(x=�漰����)) + geom_bar()+
  xlab("�᰸�漰����")+ylab("����")+
  annotate("text", x="����", y=190, label="23.4%", alpha=1.5,size = 3)+
  annotate("text", x="�Ƽ�", y=80, label="8.5%", alpha=1.5,size = 3)+
  annotate("text", x="����", y=100, label="11.5%", alpha=1.5,size = 3)+
  annotate("text", x="����", y=420, label="56.6%", alpha=1.5,size = 3)+
  scale_x_discrete(limits=c("����","����","����","�Ƽ�"))
ggsave("�漰����.png", width=12, height=8, unit="cm", dpi=300)

#3. ����ͼ4 �᰸�漰�����ʱ��仯
#��ͼ׼��
#����ÿ����᰸����
a <- data.frame(table(US20200610$�᰸������,US20200610$�漰����)) 
a <- rename(a,c(Freq="����"))
a <- rename(a,c(Var1="�᰸������"))
a <- rename(a,c(Var2="�漰����"))
a
a$�᰸������ <-  as.numeric(as.character(a$�᰸������))
total <- data.frame(����=tapply(a$����,a$�᰸������,sum),
                      �᰸������=as.numeric(as.character(names(tapply(a$����,a$�᰸������,sum)))))
total$�漰����<- c("����")
#������ӳÿ���᰸�仯�������ݿ�
a <-rbind(a,total)
save(a, file = "a.RData")
#��ͼͼ4 
ggplot(a, aes(x=�᰸������,y=����,colour=�漰����)) + 
  geom_line(size=1)
ggsave("�漰����ʱ��仯.png", width=12, height=8, unit="cm", dpi=300)

#4.�����е����ݼ���
mean(a$����[a$�漰����=="����"])



##�滪̬��

#1.�滪̬�ȵ�������� 
table(US20200610$�Ի�̬��)
prop.table(table(US20200610$�Ի�̬��))

#2.����ͼ5���滪̬�ȵ�ʱ��仯����  
attach(US20200610)
d <- data.frame(table(�᰸������,�Ի�̬��))
d
d <- rename(d,c(Freq="����"))
d$�᰸������ <-  as.character(d$�᰸������)
d$�᰸������ <-  as.numeric(d$�᰸������)

ggplot(d, aes(x=�᰸������,y=����,colour=�Ի�̬��)) + 
  geom_line(size=1)
ggsave("�Ի�̬��ʱ��仯.png", width=12, height=8, unit="cm", dpi=300)

#����
mean(d$����[d$�Ի�̬��=="����" & d$�᰸������<2009])#Bush
mean(d$����[d$�Ի�̬��=="����" & 2008 < d$�᰸������ & d$�᰸������<2017])#Obama
mean(d$����[d$�Ի�̬��=="����" & d$�᰸������> 2016])#Trump


#3. ����ͼ6����ͬ�漰�����е��滪̬��
attach(US20200610)
prop.table(table(�Ի�̬��,�漰����),2)
e <- data.frame(prop.table(table(�Ի�̬��,�漰����),2))
e <- rename(e,c(Freq="����"))
ggplot(e, aes(x=�漰����, y=����, fill=�Ի�̬��)) +
  geom_bar(stat="identity")
ggsave("�Ի�̬��X�漰����.png", width=12, height=8, unit="cm", dpi=300)

#һЩ����
prop.table(table(US20200610$�Ի�̬��[�漰����=="����"]))
prop.table(table(US20200610$�Ի�̬��[�漰����=="����"]))
prop.table(table(US20200610$�Ի�̬��[�漰����=="����"]))


#4. ����ͼ7 ���κ;������᰸�Ի�̬����ʱ��仯���� 
attach(US20200610)
US20200610$����̬��[�漰����=="����" & �Ի�̬��=="����"] <- c("��������")
US20200610$����̬��[�漰����=="����" & �Ի�̬��=="����"] <- c("���λ���")
US20200610$����̬��[�漰����=="����" & �Ի�̬��=="����"] <- c("��������")
US20200610$����̬��[�漰����=="����" & �Ի�̬��=="����"] <- c("���û���")

����̬�����<- data.frame(table(US20200610$����̬��,US20200610$�᰸������))
����̬����� <- rename(����̬�����,c(Var1="����X̬��",Var2="�᰸������",Freq="����"))
����̬�����$�᰸������ <-  as.numeric(as.character(����̬�����$�᰸������))

ggplot(����̬�����, aes(x=�᰸������,y=����,colour=����X̬��)) + 
  geom_line(size=1)
ggsave("����X̬�ȵ�ʱ��仯.png", width=12, height=8, unit="cm", dpi=300)



##����״̬

#1.�����ֵ�һЩ����
table(US20200610$����״̬[US20200610$�᰸����=="�鰸"|US20200610$�᰸����=="���Ͼ��鰸" |US20200610$�᰸����=="������"])
table(US20200610$����״̬[US20200610$�᰸����=="�鰸"|US20200610$�᰸����=="���Ͼ��鰸"]) 
prop.table(table(US20200610$����״̬[US20200610$�᰸����=="�鰸"|US20200610$�᰸����=="���Ͼ��鰸" |US20200610$�᰸����=="������"]))
prop.table(table(US20200610$����״̬[US20200610$�᰸����!="������"]))
table(US20200610$����״̬[US20200610$�᰸����!="������"])


#2.����ͼ8 �鰸�����Ͼ��鰸��������������״̬
ggplot(US20200610[US20200610$�᰸����=="�鰸"|US20200610$�᰸����=="���Ͼ��鰸" |US20200610$�᰸����=="������",], aes(x=����״̬)) + 
  geom_bar()+ ylab("����")+theme(axis.text.x = element_text(angle=30, hjust=1, vjust=1))+
  scale_x_discrete(limits=c("δͨ��ίԱ��","��ͨ��ίԱ��","��ͨ����ʼ��Ժ","��ͨ����Ժ","����ͳǩ��"))+
  annotate("text", x="δͨ��ίԱ��", y=305, label="71.4%", alpha=1.5,size = 3)+
  annotate("text", x="��ͨ��ίԱ��", y=55, label="10.2%", alpha=1.5,size = 3)+
  annotate("text", x="��ͨ����ʼ��Ժ", y=60, label="11.5%", alpha=1.5,size = 3)+
  annotate("text", x="��ͨ����Ժ", y=20, label="0", alpha=1.5,size = 3)+
  annotate("text", x="����ͳǩ��", y=40, label="6.8%", alpha=1.5,size = 3)

ggsave("�߱�����Ч������������״̬.png", width=12, height=8, unit="cm", dpi=300)


#3.����ͼ9����ʽ��������̬�ȵ�ʱ��ֲ�
����ͳǩ����᰸<- US20200610[US20200610$����״̬=="����ͳǩ��",]
attach(����ͳǩ����᰸)
table(�᰸������,�Ի�̬��)
����ͳǩ����᰸���<- data.frame(table(����ͳǩ����᰸$�Ի�̬��,����ͳǩ����᰸$�᰸������))
����ͳǩ����᰸��� <- rename(����ͳǩ����᰸���,c(Var1="�Ի�̬��",Var2="�᰸������",Freq="����"))
����ͳǩ����᰸���$�᰸������ <-  as.numeric(as.character(����ͳǩ����᰸���$�᰸������))

ggplot(����ͳǩ����᰸���, aes(x=�᰸������,y=����,fill=�Ի�̬��)) + 
  geom_bar(stat="identity")

ggsave("����ͳǩ���᰸��ʱ��ֲ�.png", width=12, height=8, unit="cm", dpi=300)


#4.����ͼ10 ��ͬ���鰸������״̬
prop.table(table(US20200610$����״̬[US20200610$�᰸����=="��ͬ���鰸"]))
table(US20200610$����״̬[US20200610$�᰸����=="��ͬ���鰸"])
ggplot(US20200610[US20200610$�᰸����=="��ͬ���鰸",], aes(x=����״̬)) + 
  geom_bar()+ ylab("����")+theme(axis.text.x = element_text(angle=30, hjust=1, vjust=1))+
  scale_x_discrete(limits=c("δͨ��ίԱ��","��ͨ��ίԱ��","��ͨ����ʼ��Ժ","��ͨ����Ժ"))+
  annotate("text", x="δͨ��ίԱ��", y=75, label="77.3%", alpha=1.5,size = 3)+
  annotate("text", x="��ͨ��ίԱ��", y=5, label="1.1%", alpha=1.5,size = 3)+
  annotate("text", x="��ͨ����ʼ��Ժ", y=23, label="20.4%", alpha=1.5,size = 3)+
  annotate("text", x="��ͨ����Ժ", y=5, label="1.1%", alpha=1.5,size = 3)

ggsave("��ͬ���鰸������״̬.png", width=12, height=8, unit="cm", dpi=300)


#5.����ͼ11 �򵥾��鰸������״̬
prop.table(table(US20200610$����״̬[US20200610$�᰸����=="�򵥾��鰸"]))

ggplot(US20200610[US20200610$�᰸����=="�򵥾��鰸",], aes(x=����״̬)) + 
  geom_bar()+ ylab("����")+theme(axis.text.x = element_text(angle=30, hjust=1, vjust=1))+
  scale_x_discrete(limits=c("δͨ��ίԱ��","��ͨ��ίԱ��","��ͨ����ʼ��Ժ"))+
  annotate("text", x="δͨ��ίԱ��", y=140, label="60.3%", alpha=1.5,size = 3)+
  annotate("text", x="��ͨ��ίԱ��", y=15, label="2.7%", alpha=1.5,size = 3)+
  annotate("text", x="��ͨ����ʼ��Ժ", y=90, label="37.1%", alpha=1.5,size = 3)

ggsave("�򵥾��鰸������״̬.png", width=12, height=8, unit="cm", dpi=300)




#����ߵ���
#����
table(US20200610$����ߵ���)

#����ͼ12 �����᰸����ʱ��仯����
US20200610$����ߵ��� <-ifelse(US20200610$����ߵ���==1,"������",US20200610$����ߵ���)
US20200610$����ߵ��� <-ifelse(US20200610$����ߵ���==0,"���͵�",US20200610$����ߵ���)


�������<- data.frame(table(US20200610$����ߵ���, US20200610$�᰸������))
������� <- rename(�������,c(Var1="����",Var2="�᰸������",Freq="����"))
�������$�᰸������ <-  as.numeric(as.character(�������$�᰸������))

ggplot(�������, aes(x=�᰸������,y=����,colour=����)) + 
  geom_line(size=1)
ggsave("�����᰸��ʱ��仯.png", width=12, height=8, unit="cm", dpi=300)


#���㣺����X̬��
prop.table(table(US20200610$����ߵ���,US20200610$�Ի�̬��),1)

#����ͼ13 ���ɶԻ�̬�ȵ�ʱ��仯����
attach(US20200610)
US20200610$����̬��[US20200610$����ߵ���=="������" & US20200610$�Ի�̬��=="����"] <- c("����������")
US20200610$����̬��[US20200610$����ߵ���=="������" & US20200610$�Ի�̬��=="����"] <- c("����������")
US20200610$����̬��[US20200610$����ߵ���=="���͵�" & US20200610$�Ի�̬��=="����"] <- c("���͵�����")
US20200610$����̬��[US20200610$����ߵ���=="���͵�" & US20200610$�Ի�̬��=="����"] <- c("���͵�����")
table(US20200610$����̬��)

����̬�����<- data.frame(table(US20200610$����̬��,US20200610$�᰸������))
����̬����� <- rename(����̬�����,c(Var1="����X̬��",Var2="�᰸������",Freq="����"))
����̬�����$�᰸������ <-  as.numeric(as.character(����̬�����$�᰸������))
����̬�����

ggplot(����̬�����, aes(x=�᰸������,y=����,colour=����X̬��)) + 
  geom_line(size=1)
ggsave("����X̬�ȵ�ʱ��仯.png", width=12, height=8, unit="cm", dpi=300)




##��4���ֵĽ����Է���

#1.��ͬ�滪̬���᰸��������״̬��Ӱ�죺��������
�߱�����Ǳ�����᰸<- US20200610[US20200610$�᰸����=="�鰸"|US20200610$�᰸����=="���Ͼ��鰸" |US20200610$�᰸����=="������",]
�򵥾��鰸 <- US20200610[US20200610$�᰸����=="�򵥾��鰸",]
��ͬ���鰸 <- US20200610[US20200610$�᰸����=="��ͬ���鰸",]

prop.table(table(�߱�����Ǳ�����᰸$�Ի�̬��,�߱�����Ǳ�����᰸$����״̬),1)
chisq.test(table(�߱�����Ǳ�����᰸$�Ի�̬��,�߱�����Ǳ�����᰸$����״̬)) 

prop.table(table(�򵥾��鰸$�Ի�̬��,�򵥾��鰸$����״̬),1)
chisq.test(table(�򵥾��鰸$�Ի�̬��,�򵥾��鰸$����״̬))

prop.table(table(��ͬ���鰸$�Ի�̬��,��ͬ���鰸$����״̬),1)
chisq.test(table(��ͬ���鰸$�Ի�̬��,��ͬ���鰸$����״̬))



#2.ʹ�õ�ģ��
 #��stateת��Ϊ��ʽ������ݸ�ʽ���洢
pstate <- pdata.frame(state,index = c("state", "congress"))


##����ģ��:ģ��1-6
summary(pglm(�����᰸���� ~ lag(exportR), 
                   data = pstate,
                   effect = "individual", model = "within", family = negbin))  
 #����
exp(-34.81)
mean(pstate$exportR,na.rm = T)

summary(pglm(�����᰸���� ~ �����ϵ, 
                   data = pstate,
                   effect = "individual", model = "within", family = negbin)) 
 #����
exp(0.3448)


summary(pglm(�����᰸���� ~ lag(exportR)+������ϵ+�����ϵ, 
                   data = pstate,
                   effect = "individual", model = "within", family = negbin)) 



summary(pglm(�����᰸���� ~ lag(exportR), 
                   data = pstate,
                   effect = "individual", model = "within", family = negbin))

summary(pglm(�����᰸���� ~ �����ϵ, 
                   data = pstate,
                   effect = "individual", model = "within", family = negbin)) 

summary(pglm(�����᰸���� ~ lag(exportR)+������ϵ+�����ϵ, 
                   data = pstate,
                   effect = "individual", model = "within", family = negbin)) 

##����ģ��:ģ��7-10

summary(pglm(�����᰸���� ~ lag(importR), 
                   data = pstate,
                   effect = "individual", model = "within", family = negbin)) 

summary(pglm(�����᰸���� ~ lag(importR)+������ϵ+�����ϵ, 
                   data = pstate,
                   effect = "individual", model = "within", family = negbin)) 



summary(pglm(�����᰸���� ~ lag(importR), 
                   data = pstate,
                   effect = "individual", model = "within", family = negbin)) 

summary(pglm(�����᰸���� ~ lag(importR)+������ϵ+�����ϵ, 
                   data = pstate,
                   effect = "individual", model = "within", family = negbin)) 



